import java.util.concurrent.locks.ReentrantLock;

/*
 * @Author: Starfish_DL
 * @Date: 2020-11-10 22:42:58
 * @LastEditors: Starfish_DL
 * @LastEditTime: 2020-11-14 21:43:07
 * @CurrentVersion: 1.0
 * @Description: 
 */

class MyCountThread extends Thread {
    private static volatile String nextF = "a";
    private static volatile int count = 1;
    private String nextFlag;
    private Object lock;

    public MyCountThread(String name, String next, Object lock) {
        super(name);
        this.nextFlag = next;
        this.lock = lock;
    }

    @Override
    public void run() {
        while (count <= 20) {
            synchronized (lock) {
                if (Thread.currentThread().getName() != nextF) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(Thread.currentThread().getName());
                    nextF = this.nextFlag;
                    count++;
                    lock.notifyAll();
                }
            }
        }
    }
}

public class Test001 {

    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object();
        MyCountThread th1 = new MyCountThread("a", "b", lock);
        MyCountThread th2 = new MyCountThread("b", "c", lock);
        MyCountThread th3 = new MyCountThread("c", "a", lock);

        th1.start();
        th2.start();
        th3.start();
    }
}